package com.weds.edjk.service;

import com.weds.core.base.BaseService;
import com.weds.edjk.entity.KtRecordEntity;
import com.weds.edjk.entity.UserEntity;
import com.weds.edjk.mapper.KtRecordMapper;
import com.weds.edjk.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author zcz
 * @Description 人员基本信息管理
 * @Date 2020-6-12
 */
@Service
public class PersonMonitorService extends BaseService {


    @Value("${weds.scm.root-path}")
    private String rootPath;

    @Value("${weds.scm.photo-path}")
    private String photoPath;

    @Value("${weds.scm.frame-path}")
    private String framePath;

    @Value("${weds.scm.frame-name-prefix}")
    private String frameNamePrefix;

    @Resource
    private KtRecordMapper ktRecordMapper;

    /**
     * 当前人员监控信息
     * @return
     */
    public Map<String,Object> getCurrentMonitor(){
        Map<String, Object> map = new HashMap<>();
        // 查询最近一条门禁记录
        KtRecordEntity currentKtJl = ktRecordMapper.selectCurrentKtJl();
        // 常出没地
        List<KtRecordEntity> ktRecordEntities = null;
        // 用户基本信息
        UserEntity user = null;
        // 即时照片
        String frameUrl = null;
        // 档案照片
        String photoUrl = null;
        // 档案照片
        String frame = null;
        if(Objects.nonNull(currentKtJl)){
            user = ktRecordMapper.selectUserByUserSerial(currentKtJl.getUserSerial() + "");
            String date = DateUtils.getStr(currentKtJl.getSj(), DateUtils.YYYY_MM_DD);
            frame = rootPath+"/"+framePath+"/"+currentKtJl.getDevSerial()+"/"+date+"/"+frameNamePrefix+currentKtJl.getJlzpSerial()+".jpg";
            ktRecordEntities = ktRecordMapper.selectTopTenKtJlByUserSerial(currentKtJl.getUserSerial() + "");
            for(KtRecordEntity ktRecordEntity:ktRecordEntities){
                String d = DateUtils.getStr(currentKtJl.getSj(), DateUtils.YYYY_MM_DD);
                frameUrl = rootPath+"/"+framePath+"/"+ktRecordEntity.getDevSerial()+"/"+d+"/"+frameNamePrefix+ktRecordEntity.getJlzpSerial()+".jpg";
                ktRecordEntity.setFrameUrl(frameUrl);
            }
            String s = StringUtils.substring(currentKtJl.getUserSerial() + "", 0, 5);
            photoUrl = rootPath+"/"+photoPath+"/"+s+"/"+currentKtJl.getUserSerial()+".jpg";
        }
        map.put("frame",frame);
        map.put("user",user);
        map.put("kts",ktRecordEntities);
        map.put("photoUrl",photoUrl);
        return map;
    }

    /**
     * 今日访问分类
     * @return
     */
    public Map<String,Object> getTodayVisits (){
        String nowDate = DateUtils.getNowDate();
        int warnCount = ktRecordMapper.selectWarnCount(nowDate);
        int insideCount = ktRecordMapper.selectInsideCount(nowDate);
        int ethnicMinorityCount = ktRecordMapper.selectEthnicMinorityCount(nowDate);
        int visCount = ktRecordMapper.selectVisCount(nowDate);
        Map<String, Object> map = new HashMap<>();
        map.put("warnCount",warnCount);
        map.put("insideCount",insideCount);
        map.put("ethnicMinorityCount",ethnicMinorityCount);
        map.put("visCount",visCount);
        return map;
    }

    /**
     * 今日预警列表
     * @return
     */
    public List<KtRecordEntity> selectWarn (){
        String nowDate = DateUtils.getNowDate();
        List<KtRecordEntity> recordEntities = ktRecordMapper.selectWarn(nowDate);
        for(KtRecordEntity r:recordEntities){
            String s = StringUtils.substring(r.getUserSerial() + "", 0, 5);
            String photoUrl = rootPath+"/"+photoPath+"/"+s+"/"+r.getUserSerial()+".jpg";
            r.setPhotoPath(photoUrl);
        }
        return recordEntities;
    }

    /**
     * 今日出入历史记录照片
     * @param userSerial
     * @return
     */
    public String getPhotoPath(String userSerial) {
        if (StringUtils.isBlank(userSerial)) {
            return "";
        }
        String s = StringUtils.substring(userSerial, 0, 5);
        String photoUrl = rootPath + "/" + photoPath + "/" + s + "/" + userSerial + ".jpg";
        return photoUrl;
    }
}
